package org.dreamwork.jasmine2.web.html;

import org.dreamwork.jasmine2.engine.HttpContext;
import org.dreamwork.jasmine2.events.EventException;
import org.dreamwork.jasmine2.parser.Tag;
import org.dreamwork.jasmine2.util.StringConverter;
import org.dreamwork.jasmine2.web.IWebControl;
import org.dreamwork.jasmine2.web.controls.WebContainer;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-1-30
 * Time: 22:51:45
 */
public class GenericHtmlControl extends WebContainer {
    private static final String[] singleTag = {
            "br", "input", "hr", "img", "image", "link", "meta"
    };


    public GenericHtmlControl () {
        viewStateEnabled = false;
    }

    public GenericHtmlControl (String tagName) {
        htmlTagName = tagName;
        viewStateEnabled = false;
    }

    public GenericHtmlControl (Tag tag) {
        htmlTagName = tag.getName ();
        String[] names = tag.getAttributeNames ();
        for (String name : names) {
            setAttribute (name, tag.getAttribute (name));
        }
    }

    public IWebControl copy () {
        GenericHtmlControl copy = new GenericHtmlControl (htmlTagName);
        for (Object o : attrs.keySet ()) {
            String key = (String) o;
            copy.attrs.setProperty (key, attrs.getProperty (key));
        }

        for (Object o : dynamicAttributes.keySet ()) {
            String key = (String) o;
            copy.dynamicAttributes.put (key, dynamicAttributes.get (key));
        }
        copy.page = page;
        if (name != null)
            copy.setAttribute ("name", name);
        return copy;
    }

    @Override
    public void onPrerender (IWebControl sender, HttpContext context) throws EventException {
        if ("html".equalsIgnoreCase (htmlTagName)) {
            if (getAttribute ("xmlns") == null)
                setAttribute ("xmlns", "http://www.w3.org/1999/xhtml");
        }

        for (String key : dynamicAttributes.keySet ()) {
            if (attrs.containsKey (key)) continue; // databind has process this dynamic attribute!
            String expression = dynamicAttributes.get (key);
            try {
                Object obj = page.eval (expression);
                if (obj != null && !StringConverter.isEmpty (obj.toString ()))
                    attrs.setProperty (key, obj.toString ().trim ());
            } catch (Exception e) {
                e.printStackTrace ();
            }
        }
        super.onPrerender (sender, context);
    }

    protected void beginTag (PrintWriter writer) throws IOException {
        if (isSingleTag (htmlTagName)) {
            writer.write ("<" + htmlTagName);
            renderAttributes (writer);
            writer.write ("/>");
        } else
            super.beginTag (writer);
    }

    protected void endTag (PrintWriter writer) {
        if (!isSingleTag (htmlTagName))
            super.endTag (writer);
    }

    private boolean isSingleTag (String tagName) {
        for (String s : singleTag) {
            if (s.equalsIgnoreCase (tagName)) return true;
        }
        return false;
    }
}